//服务器地址
// const service = "https://oto.uxdj.com.cn/"
const service = "https://oto.niyuedaojia.cn"		//正式
// const service = "https://oto.uxdj.com.cn/"		//测试
// const service = "http://192.168.2.20:30227/"
module.exports = (params) => { 
    let url = params.url; 
    let method = params.method; 
    let header = params.header || { };
    let data = params.data || { };
	let is_login = data.is_login == undefined?true:data.is_login;
	let is_force = data.is_force || false;
	let is_phone = data.is_phone == undefined ? true : data.is_phone;
	console.log(header,'---------------------------------请求头header')
    // 请求方式 GET POST
    if (method) { 
		let from = uni.getStorageSync('from')
        method = method.toUpperCase(); // 小写转大写
        if (method == "POST") { 
            header = { 
                "content-type": "application/json", 
			} 
        }
		// #ifdef APP-PLUS
		header.from = from
		// #endif
    }
	//请求头token 
	if(params.header && params.header.token){
		header.Authorization = `Bearer ${params.header.token}`
	}else{
		let token = uni.getStorageSync('token')
		header.Authorization = `Bearer ${token}` 
	}
	// header.Authorization  = `Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJkYW9qaWEud2VhcHBlLmNuIiwiYXVkIjoiZGFvamlhLndlYXBwZS5jbiIsImlhdCI6MTY2ODIzMzcxMywibmJmIjoxNjY4MjMzNzEzLCJleHAiOjE2NzA4MjU3MTMsImRhdGEiOnsiaWQiOjM3fX0.1FTMSPFhNF6YrG6w-bJJjbC2NqSbOhJeAdjdc_SdrLo`
    // 发起网络请求
    uni.request({ 
        url: service + url,
        method: method || "GET",
        header: header,
        data: data,
        dataType: "json",
        sslVerify: false, // 是否验证ssl证书
        success: res => { 
            console.log(res)
            if (res.data.code && res.data.code != 200) { 
				console.log((is_login || is_force) && res.data.code == 401)
				if((is_login || is_force) && res.data.code == 401){
					uni.showToast({
					    title:'请登录',
					    icon:'none'
					})
					setTimeout(()=>{
						// #ifdef H5
						if(navigator.userAgent.toLowerCase().indexOf('micromessenger') != -1){
							uni.reLaunch({
								url:'/pages/login/index'
							})
						}else{
							let url = ''
							if(is_phone){
								url = '/pages/login/login'
							}else{
								url = '/pages/login/index'
							}
							setTimeout(()=>{
								uni.reLaunch({
									url:url
								})
							},500)
						}
						// #endif
						// #ifdef APP-PLUS
						uni.reLaunch({
							url:'/pages/login/login'
						})
						// #endif
					},2000)
					uni.removeStorageSync('token')
				}else if(res.data.code != 401){
					// api错误
					uni.showToast({ 
					    title:res.data.msg,
					    icon:'none'
					})
				}
                return;
            }
            typeof params.success == "function" && params.success(res.data);
        },
        // fail: err => { 
        //     uni.showToast({ 
        //         title:'系统错误',
        //         icon:'none'
        //     })
        //     typeof params.fail == "function" && params.fail(err.data);
        // },
        complete: (e) => { 
            console.log("请求完成");
            // uni.hideLoading()
            typeof params.complete == "function" && params.complete(e.data);
            return;
        }
    })
}